package com.example.bookmall.controller.bookmallSystem;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bookmall.entity.Result;
import com.example.bookmall.entity.Book;
import com.example.bookmall.entity.Category;
import com.example.bookmall.service.BookService;
import com.example.bookmall.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
//TODO：如果图书详情被删除，那么用户就无法查看该图书的详情了
//TODO：如果购物车里面有书但是被下架了，那么用户就无法查看该图书的详情了
@RestController
@RequestMapping("/books")
public class BookController {

    @Autowired
    private BookService bookService;

    @Autowired
    private CategoryService categoryService;
    //搜索图书 用户 只能查询上架的图书
    @GetMapping("/search")
    public Result searchBooks(@RequestParam(required = false) String keyword,
                              @RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Book> books = bookService.searchBooks(keyword, page, pageSize);
        return Result.success(books);
    }

    @GetMapping("/category")
    public Result getBooksByCategory(@RequestParam String categoryName) {
        // 检查分类名称是否为空
        if (categoryName == null || categoryName.trim().isEmpty()) {
            return Result.error("分类名称不能为空");
        }

        // 去除分类名称前后空格
        categoryName = categoryName.trim();

        // 根据分类名称查询分类ID
        Category category = categoryService.seCategoryByName(categoryName);
        if (category == null) {
            return Result.error("分类不存在");
        }

        // 使用分类ID查询图书
        List<Book> books = bookService.getBooksByCategory(category.getCategoryId());
        return Result.success(books);
    }


    //获取图书详情根据bookid
    @GetMapping("/{bookId}")
    public Result getBookDetails(@PathVariable Integer bookId) {
        Book book = bookService.getBookDetails(bookId);
        return Result.success(book);
    }

    //获取所有图书
    @GetMapping("/list")
    public Result listBooks(@RequestParam(required = false) String keyword,
                            @RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer pageSize) {
        //TODO：如果传回来是一个string的分类，还需要在分类表里面确认分类id 待定
        //TODO: 获取所有图书 默认查询所有 管理员端使用
        IPage<Book> books = (IPage<Book>) bookService.searchBooks(keyword, page, pageSize);
        return Result.success(books);
    }

    //获取最新几本书 用于添加在上新的页面
    @GetMapping("/new")
    public Result getNewBooks(@RequestParam(defaultValue = "8") Integer count) {
        List<Book> books = bookService.getNewBooks(count);
        return Result.success(books);
    }

    //获取推荐 根据销量
    @GetMapping("/recommend")
    public Result getRecommendBooks(@RequestParam(defaultValue = "8") Integer count) {
        List<Book> books = bookService.getRecommendBooks(count);
        return Result.success(books);
    }


    //获取轮播书籍
    @GetMapping("/banner")
    public Result getBannerdBooks(@RequestParam(defaultValue = "8") Integer count) {
        List<Book> books = bookService.getBannerdBooks(count);
        return Result.success(books);
    }

}    